<?php
namespace Home\Controller;
class CartController extends CommonController{ 
	//判断用户是否已经登录检查
	public function __construct(){
		parent::__construct();
//		var_dump($this->userInfo);
		if ($this->userInfo === false) {
			$this->error('请先登录。',U('User/login'));
		}
	}
	
	//获取登录用户的购物车列表
	public function index(){
		$userId = $this->userInfo['userId'];
		$userdata=M('user')->where("UserId=$userId")->find();
		$ordercol=M('cart')->where("user_UserId=$userId")->count();
		$data['cart'] = D('cart')->getList($userId);//构造getList方法
		$this->assign($data);
		$this->assign($userdata);
		$this->display();
	}
	
	//添加到购物车
	public function add(){
		//获取商品属性
		$gid = I('get.gid',0,'int');
		$num = I('get.num',0,'int');
		$color = I('get.color','');
		$version = I('get.version','');
		
		$userId = $this->userInfo['userId'];
		$rst = D('cart')->addCart($userId,$gid,$color,$version,$num); //构造addCart方法
		if ($rst == false) {
			$this->error('添加购物车失败');
		}
		$this->success('添加购物车成功',U('Cart/index'));
	}
	
	
	//删除购物车商品
	public function del(){
		//获取商品的主键信息
		$userId = I('get.userId','0','int');
		$gid = I('get.gid','0','int');
		$color = I('get.color'); //出现乱码 中文是不能删除
		$version = I('get.version');
//		var_dump($gid);
//		var_dump($color);
//		var_dump($version);
//		var_dump($userId);	
		//$userId = $this->userInfo['userId'];
		$rst = M('cart')->where("user_UserId=$userId and goods_GoodId=$gid and goods_Version='$version' and goods_Color='$color' ")->delete();
		//var_dump($rst);
		if ($rst == false) {
			$this->error('删除失败');
		}
        $this->redirect('Cart/index');
	}
}

?>